package util;

import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class Utilidades {

	public static void convertirMayuscula(Component comp) {
		comp.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent ke) {
				char c = ke.getKeyChar();
				Character.toUpperCase(c);
				ke.setKeyChar(Character.toUpperCase(c));
			}
		});

	}

	public static void soloNumeros(Component comp) {
		comp.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent ke) {
				char c = ke.getKeyChar();
				if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)))) {
					ke.consume();
				}
			}
		});
	}

	public static boolean campoVacio(JDialog dialog, JTextComponent text,
			String campo) {
		    String valor = text.getText();
		if (valor.equalsIgnoreCase("")) { 
			//dialog.getToolkit().beep();
			JOptionPane.showMessageDialog(dialog, "El campo " + campo
			+ " esta vacio", "Omision de Datos", JOptionPane.ERROR_MESSAGE);
			return true;
		} else
			return false;
	}

	public static boolean seleccion(JTable tabla, JDialog dialog,
			String registro) {
		if (tabla.getSelectedRow() != -1)
			return true;
		else { 
			//dialog.getToolkit().beep();
			JOptionPane.showMessageDialog(dialog, "Seleccione " + registro,
			"Seleccionar", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}

	public static void limitarCaracteres(final JTextField comp, final int cantidad) {
		comp.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent ke) {
				if (comp.getText().length() >= cantidad) ke.consume();
			}
		});
	}

	public static String convertirFechaParaSQL(Date fecha) {
		if (fecha == null) return "NULL";
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return "'" + sdf.format(fecha) + "'";
	}
}